<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\User;

use App\Constants\Constants;
use App\Dao\IntegralDetailDao;
use App\Dao\UserDao;
use App\Kernel\Util\UserJwtInstance;
use App\Service\Service;

use Hyperf\Di\Annotation\Inject;

/**
 * 积分服务
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class IntegralDetailService extends Service
{

    /**
     * 用户
     * @Inject
     *
     * @var UserDao
     */
    private $user_dao;

    /**
     * 积分详情
     * @Inject
     *
     * @var IntegralDetailDao
     */
    public $integral_detail_dao;

    /**
     * 积分列表
     * @param string $start_time
     * @param int $type
     * @return mixed
     */
    public function integral(string $start_time, int $type)
    {
        // 本月第一天
        $first = date('Y-m-01 0:0:0', strtotime($start_time));
        // 本月最后一天
        $last = date('Y-m-d 23:59:59', strtotime("$first +1 month -1 day"));
        //本月
        //if( $type == 1 ) {

        $whereMap['user_id'] = uid();
        $whereMap['status'] = $this->integral_detail_dao::CONSUME_INTEGRAL;

        $result = $this->integral_detail_dao->integral($whereMap, strtotime($first), strtotime($last));

        if( !$result ) {
             $this->error();
        }

        $result->makeHidden('lower_id');
        $return = [];
        foreach ( $result  as $key => $value) {
            $return[$key]['name'] = $value['goods_name'] ?? '';
            $return[$key]['logo'] = $value['goods_pic'] ?? '';
            $return[$key]['order_no'] = $value['order_no'] ?? '';
            $return[$key]['amount'] = $value['amount'] ?? '';
            $return[$key]['integral'] = $value['integral'] ?? '';
            $return[$key]['create_time'] = date('Y-m-d H:i', $value['create_time']);
            $return[$key]['num'] = $value['type'] == 1?$value['num']:"-".$value['num'];
        }

        return $return;
       // }
        //往月

    }

    /**
     * 用户积分明细 - 分销会员获得
     *
     * @return mixed
     */
    public function get()
    {
        $integral_detail = [];

        $get = $this->user_dao->get([
            'parent_id' => uid(),
            'group_id' => $this->user_dao::USER_MEMBER
        ]);
        $integral_detail = [];
        $integral_detail['integral_list'] = [];
        foreach ( $get as $key => $value ) {
            $integral_detail['integral_list'][$key]['id'] = $value['id'];
            $integral_detail['integral_list'][$key]['name'] = $value['name'];
            $integral_detail['integral_list'][$key]['phone'] = $value['phone'];
            $integral_detail['integral_list'][$key]['avatar'] = $value['avatar'];
            $integral_detail['integral_list'][$key]['cumulatived_total_integral'] = $value['cumulatived_money'] ?? 0.00;
            $integral_detail['integral_list'][$key]['cumulatived_integral'] = $this->integral_detail_dao->sumIntegralBalance([
                'user_id' => uid(),
                'lower_id' => $value['id'],
                'type' => 1,
                'status' => 2,
            ]);
        }

        $integral_detail['parent_count'] = $this->user_dao->count([
            'parent_id' => uid(),
            'group_id' => $this->user_dao::USER_MEMBER
        ]);

        $integral_detail['integral_total'] = $this->getIntegralBalanceByShare( uid() );

        return $integral_detail;
    }

    /**
     * 用户积分修改
     * @param int $user_id
     * @return mixed
     */
    public function updateIntegralBalance(int $user_id)
    {
        $balance_income = $this->sumIntegralBalance( [
            'user_id' => $user_id,
            'type' => 1
        ] );
        $balance_expenditure = $this->sumIntegralBalance( [
            'user_id' => $user_id,
            'type' => 2
        ] );

        $balance = $balance_income - $balance_expenditure;

        $balanceShare = $this->sumIntegralBalance( [
            'user_id' => $user_id,
            'status' => 2,
            'type' => 1
        ] );

        $balanceConsume = $this->sumIntegralBalance( [
            'user_id' => $user_id,
            'status' => 1,
            'type' => 1
        ] );

        $this->user_dao->retrieve(
            [
                'integral' => $balance
            ]
            ,
            [
                'id' => $user_id
            ]
        );

        $this->redis->hSet('txh:user:'.$user_id, 'integral', $balance); //总积分

        $this->redis->hSet('txh:user:share:'.$user_id, 'integral', $balanceShare); //分润

        $this->redis->hSet('txh:user:consume:'.$user_id, 'integral', $balanceConsume); //消费

        return [];
    }

    /**
     * 获取用户实时缓存积分 --- 总积分
     * @param int $user_id
     * @return string
     */
    public function getIntegralBalance(int $user_id)
    {
        $integral =  $this->redis->hGet( 'txh:user:'.$user_id, 'integral' );

        return $integral ?$integral:0.0;
    }

    /**
     * 获取用户实时缓存积分 --- 分润积分
     * @param int $user_id
     * @return mixed
     */
    public function getIntegralBalanceByShare(int $user_id)
    {
        $integral =  $this->redis->hGet( 'txh:user:share:'.$user_id, 'integral' );

        return $integral ?$integral:"0.0";
    }

    /**
     * 获取用户实时缓存积分 --- 消费积分
     * @param int $user_id
     * @return mixed
     */
    public function getIntegralBalanceByConsume(int $user_id)
    {
        $integral =  $this->redis->hGet( 'txh:user:consume:'.$user_id, 'integral' );

        return $integral ?$integral:0.0;
    }

    /**
     * 实时计算用户积分 --- 按条件
     * @param array $map
     * @return mixed
     */
    public function sumIntegralBalance(array $map)
    {
        return $this->integral_detail_dao->sumIntegralBalance( $map );
    }
}